﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using VisualWip.PresentationModel;

namespace VisualWip
{
    /// <summary>
    /// Interaction logic for SizeAndPositionAdornerView.xaml
    /// </summary>
    public partial class SizeAndPositionAdornerView : UserControl
    {
        private ColumnViewModelBase _columnViewModel;

        public SizeAndPositionAdornerView()
        {
            InitializeComponent();
            DataContextChanged += SizeAndPositionAdornerViewDataContextChanged;
        }

        private void SizeAndPositionAdornerViewDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            _columnViewModel = DataContext as ColumnViewModelBase;
        }

        private void nwThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            ReSizeAndMove(e, Direction.NorthWest);
        }


        private void swThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            ReSizeAndMove(e, Direction.SouthWest);
        }

        private void neThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            ReSizeAndMove(e, Direction.NorthEast);
        }

        private void seThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            ReSizeAndMove(e, Direction.SouthEast);
        }

        private void moveThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            ReSizeAndMove(e, Direction.MoveHandle);
        }

        private void ReSizeAndMove(DragDeltaEventArgs e, Direction direction)
        {
            if (_columnViewModel != null)
            {
                int positionX = _columnViewModel.PositionX;
                int positionY = _columnViewModel.PositionY;
                int width = _columnViewModel.Width;
                int height = _columnViewModel.Height;

                if (direction == Direction.NorthWest || direction == Direction.SouthWest ||
                    direction == Direction.MoveHandle)
                    positionX = positionX + (int)Math.Floor(e.HorizontalChange);

                if (direction == Direction.NorthWest || direction == Direction.NorthEast ||
                    direction == Direction.MoveHandle)
                    positionY = positionY + (int)Math.Floor(e.VerticalChange);

                if ((direction == Direction.NorthWest || direction == Direction.SouthWest))
                    width = width - (int)Math.Floor(e.HorizontalChange);
                else if (direction == Direction.NorthEast || direction == Direction.SouthEast)
                    width = width + (int)Math.Floor(e.HorizontalChange);

                if ((direction == Direction.NorthWest || direction == Direction.NorthEast))
                    height = height - (int)Math.Floor(e.VerticalChange);
                else if (direction == Direction.SouthWest || direction == Direction.SouthEast)
                    height = height + (int)Math.Floor(e.VerticalChange);

                Debug.WriteLine("Adorner Position X: {0}", positionX);
                Debug.WriteLine("Adorner Position Y: {0}", positionY);
                Debug.WriteLine("Adorner Width: {0}", width);
                Debug.WriteLine("Adorner Height: {0}", height);

                _columnViewModel.PositionX = positionX;
                _columnViewModel.PositionY = positionY;
                _columnViewModel.Width = width;
                _columnViewModel.Height = height;
            }
        }

        private void NwThumb_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_columnViewModel == null || !_columnViewModel.SnapToGrid)
                return;

            int positionX = _columnViewModel.PositionX;
            int positionY = _columnViewModel.PositionY;

            int xSnap = GetSnap(positionX);
            int ySnap = GetSnap(positionY);

            _columnViewModel.PositionX += xSnap;
            _columnViewModel.PositionY += ySnap;
            _columnViewModel.Width -= xSnap;
            _columnViewModel.Height -= ySnap;
        }

        private void SwThumb_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_columnViewModel == null || !_columnViewModel.SnapToGrid)
                return;

            int positionX = _columnViewModel.PositionX;
            int height = _columnViewModel.Height;

            int xSnap = GetSnap(positionX);
            int heightSnap = GetSnap(height);

            _columnViewModel.PositionX += xSnap;
            _columnViewModel.Width -= xSnap;
            _columnViewModel.Height += heightSnap;
        }

        private void NeThumb_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_columnViewModel == null || !_columnViewModel.SnapToGrid)
                return;

            int width = _columnViewModel.Width;
            int positionY = _columnViewModel.PositionY;

            int widthSnap = GetSnap(width);
            int ySnap = GetSnap(positionY);

            _columnViewModel.PositionY += ySnap;
            _columnViewModel.Width += widthSnap;
            _columnViewModel.Height -= ySnap;
        }

        private void SeThumb_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_columnViewModel == null || !_columnViewModel.SnapToGrid)
                return;

            int width = _columnViewModel.Width;
            int height = _columnViewModel.Height;

            int widthSnap = GetSnap(width);
            int heightSnap = GetSnap(height);

            _columnViewModel.Width += widthSnap;
            _columnViewModel.Height += heightSnap;
        }

        private void MoveThumb_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_columnViewModel == null || !_columnViewModel.SnapToGrid)
                return;

            int positionX = _columnViewModel.PositionX;
            int positionY = _columnViewModel.PositionY;

            int xSnap = GetSnap(positionX);
            int ySnap = GetSnap(positionY);

            _columnViewModel.PositionX += xSnap;
            _columnViewModel.PositionY += ySnap;
        }

        private int GetSnap(int value)
        {
            int snapGridSize = 10;
            int snap = value % snapGridSize;

            if (snap <= snapGridSize / 2.0)
                snap *= -1;
            else
                snap = snapGridSize - snap;
            return snap;
        }

    }
}